package domain.models;

import java.util.ArrayList;
import java.util.List;

import controllers.DomainController;
import controllers.MyObserver;

import domain.Player;

	

public class RegisteredPlayersTableModel extends MyTableModel implements MyObserver{

	private String[] columnHeaders = {"Voornaam", "Naam"};
	private List<Player> data = new ArrayList<Player>();
	
	
	public RegisteredPlayersTableModel(DomainController domainController) {
		super(domainController);
		super.columnHeaders = columnHeaders;
		this.data = domainController.getRegisteredPlayers();
		domainController.getCurrentTournament().addObserver(this);
	}
	
	@Override
	public Object getValueAt(int arg0, int arg1) {
		return data.get(arg0).toArray()[arg1]; 
	}
	
	
	public String[] getColumnHeaders() {
		return columnHeaders;
		
	}

	@Override
	public int getRowCount() {
		return data.size();
	}
	
	public String getRow(int rowIndex)
	{
		return data.get(rowIndex).toString();
	}
	
	public void addRow(Player player)
	{  
          data.add(player);
	      this.fireTableDataChanged();  
	         
	} 

	@Override
	public void update(String adressee) {
		if(this.getClass().toString().contains(adressee))
		{
			this.data = domainController.getRegisteredPlayers();
			fireTableDataChanged();
		}	
	}
	
	@Override
	public boolean isCellEditable(int row, int col)
	{
		return true;
	}

	@Override
	public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
		
		/*//Player player = (Player) aValue;
		String firstName = (String) this.getValueAt(rowIndex, 0);
		String lastName = (String) this.getValueAt(rowIndex, 1);
		String name = aValue.toString();
		System.out.println("setvalueat: "+name);

		String namesAfterSplit[] = name.split("\\s+",2);
		Player newPlayerName = new Player(namesAfterSplit[0], namesAfterSplit[1]);
		
		for(Player p : data)
		{
			if(p.getFirstname() == firstName && p.getLastname() == lastName)
			{
				int indexCurrentPlayer = data.indexOf(p);
				
				data.set(indexCurrentPlayer, newPlayerName);
			}
		}*/
		
		
	}

	
}
